const { ccclass, property } = cc._decorator;
import EventMgr from "./EventMgr";
import MatchGameEventType from "./MatchGameEventType";
import frameworkManager from "./frameworkManager";
import TimeUtils from "./TimeUtils";
import MatchGameData from "./MatchGameData";
import MatchGameDataMgr from "./MatchGameDataMgr";

@ccclass
export default class MatchGameFill extends cc.Component {
    onLoad() {
        EventMgr.listen(MatchGameEventType.ADD_ALL_FILL, this.addAllFill3, this);
    }

    start() {}

    addAllFill2(e) {
        var t = e.callTime,
            a = e.tTime,
            o = t + 1e3 * a;
        MatchGameData.allFillArr.push({
            callTime: t,
            tTime: a,
            realTime: o
        });
        MatchGameData.allFillArr.sort(function(e, t) {
            return e.realTime - t.realTime;
        });
    }

    addAllFill3(e) {
        if (0 != e.tTime) {
            MatchGameDataMgr.AllFill();
        }
    }

    lateUpdate() {
        if (MatchGameData.allFillArr.length) {
            var e = TimeUtils.getTimestamp(),
                t = MatchGameData.allFillArr[0];
            if (0 != MatchGameData.lastAllFillTime) {
                if (t.realTime - MatchGameData.lastAllFillTime > MatchGameData.nextFillInterval) {
                    frameworkManager.log("下落处理，落", MatchGameData.nextFillInterval);
                    if (t.realTime - e <= 0) {
                        MatchGameData.allFillArr.shift();
                        MatchGameDataMgr.AllFill();
                        MatchGameData.lastAllFillTime = e;
                    }
                } else {
                    frameworkManager.log("下落处理，不落");
                    for (var a = MatchGameData.allFillArr, o = -1, i = a.length - 1; i >= 0; i--) {
                        if (a[i].realTime - MatchGameData.lastAllFillTime > MatchGameData.nextFillInterval) {
                            o = i;
                            break;
                        }
                    }
                    if (o >= 0) {
                        MatchGameData.allFillArr = MatchGameData.allFillArr.slice(o);
                    } else {
                        MatchGameData.allFillArr = MatchGameData.allFillArr.slice(-1);
                        MatchGameData.allFillArr[0].realTime =
                            MatchGameData.lastAllFillTime +
                            MatchGameData.nextFillInterval +
                            10;
                    }
                }
            } else {
                MatchGameData.lastAllFillTime = e;
                MatchGameData.allFillArr.shift();
                MatchGameDataMgr.AllFill();
                frameworkManager.log("下落处理,first");
            }
        }
    }
}
